package hihocoder;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main{
	private class Coin{
		public Coin(int a,int p){
			age = a;
			price = p;
		}
		public int price;
		public int age;
	}

	private class State{
		public State(int p,int b,int t){
			positions = p;
			blanks = b;
			totalprice = t;
		}
		public int positions;
		public int blanks;
		public int totalprice;
	}
	
	double[][] data;
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in 

);
		Comparator<Coin> comparator = new Comparator<Coin>() {
			public int compare(Coin c1,Coin c2){
				if (c1.age>=c2.age) {
					return -1;
				}else{
					return 1;
				}
			}
		};
		int T = scanner.nextInt();
		for(int t=0;t<T;t++){
			int ans = Integer.MIN_VALUE;
			int N = scanner.nextInt();
			int R = scanner.nextInt();
			int positions = (int) Math.ceil((double)N/R);
			int[] A = new int[N];
			int[] P = new int[N];
			for(int n=0;n<N;n++){
				A[n] = scanner.nextInt();
			}
			for(int n=0;n<N;n++){
				P[n] = scanner.nextInt();
			}
			Queue<Coin> coins = new PriorityQueue<>(N,comparator); 
			for(int n=0;n<N;n++){
				coins.add(new Main().new Coin(A[n], P[n]));
			}
			
			List<State> stateList = new ArrayList<>();
			stateList.add(new Main().new State(positions, 0, 0));
			while(stateList.size()>0){
				List<State> nextList = new ArrayList<>();
				Coin coin = coins.poll();
				for(int i=0;i<stateList.size();i++){
					State state = stateList.get(i);
					if(state.positions==0){
						ans = ans>state.totalprice?ans:state.totalprice;
						continue;
					}
					nextList.add(new Main().new State(state.positions-1, state.blanks+R, state.totalprice+coin.price));
					if(state.blanks!=0){
						nextList.add(new Main().new State(state.positions, state.blanks-1, state.totalprice));
					}
				}
				stateList = nextList;
			}
			System.out.println(ans);
			
		}
		scanner.close();
    }
}


